// formPackage/pages/team/personalPerformance/personalPerformance.js
import { getMyPerformanceList } from "../../../../service/team_service"
import { queryPersonalCompanyPer } from "../../../../service/commission_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    switchArr: [
      {
        title: "全部",
        key: "-1",
      },
      {
        title: "结算中",
        key: "1",
      },
      {
        title: "已结算",
        key: "2",
      },
    ],
    topTypeIndex: 0,
    dataList: [

    ],
    avatar: "",
    nickName: "",
    settlementStatus: "-1",//业绩佣金状态,
    totalMoney: 0,
    settlementEnd: 0,
    settlementIng: 0,
    wxUserId: "",
    companyNumber: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.wxUserId) {
      this.setData({
        wxUserId: options.wxUserId,
        companyNumber: options.companyNumber
      })
      this.queryPersonalCompanyPer();
    } else {
      this.setData({
        avatar: app.globalData.avatar,
        nickName: app.globalData.name || app.globalData.nickName
      })
      this.getMyPerformanceList();
    }

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //顶部类型切换:
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.switchArr[index];
    let st = item.key;

    this.setData({
      topTypeIndex: index,
      settlementStatus: st
    })
    if (this.data.wxUserId) {
      this.queryPersonalCompanyPer();
    } else {
      this.getMyPerformanceList();
    }

  },
  getMyPerformanceList() {
    wx.showLoading();
    getMyPerformanceList(this.data.settlementStatus).then(res => {
      wx.hideLoading();
      console.log("JJLog:个人业绩是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          totalMoney: res.data.totalMoney || 0,
          settlementIng: res.data.settlementIng || 0,
          settlementEnd: res.data.settlementEnd || 0,
          dataList: res.data.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:getMyPerformanceList", err);
    })
  },

  queryPersonalCompanyPer() {
    wx.showLoading();
    queryPersonalCompanyPer(this.data.wxUserId, this.data.companyNumber, this.data.settlementStatus).then(res => {
      wx.hideLoading();
      console.log("JJLog:个人业绩是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          totalMoney: res.data.totalMoney || 0,
          settlementIng: res.data.settlementIng || 0,
          settlementEnd: res.data.settlementEnd || 0,
          dataList: res.data.data || [],
          avatar: res.data.avatar,
          nickName: res.data.name
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryPersonalCompanyPer", err);
    })
  },

  //跳转到结算详情:
  pushToDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/formPackage/pages/team/personalPerformanceDetail/personalPerformanceDetail?id=' + id,
    })
  },
  //跳转到业绩排行榜:
  pushToRankDetail() {
    wx.navigateTo({
      url: '/formPackage/pages/team/performanceRanks/performanceRanks',
    })
  },





})